/*
 *              Copyright (C) 2011 The MusicMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *            http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.musicmod.android.activity;

import org.musicmod.android.Constants;
import org.musicmod.android.R;
import org.musicmod.android.utils.MusicUtils;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;

public class BaseConfig extends PreferenceActivity implements Constants,
		OnPreferenceClickListener {

	private ProgressDialog progressDialog = null;
	private Handler mHandler = null;
	private final int PROGRESS_DIALOG = 1;
	private final int PROCESS_STARTED = 2;
	private final int PROCESS_FINISHED = 3;
	private Preference rescan, plugins_manager;
	private CheckBoxPreference shake_enabled;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setVolumeControlStream(AudioManager.STREAM_MUSIC);

		mHandler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case PROCESS_STARTED:
					showDialog(PROGRESS_DIALOG);
					break;
				case PROCESS_FINISHED:
					removeDialog(PROGRESS_DIALOG);
					break;
				}
			}
		};

		addPreferencesFromResource(R.xml.base_config);

		rescan = (Preference) findPreference(KEY_RESCAN_MEDIA);

		rescan.setOnPreferenceClickListener(this);

		plugins_manager = (Preference) findPreference(KEY_PLUGINS_MANAGER);
		plugins_manager.setOnPreferenceClickListener(this);

		shake_enabled = (CheckBoxPreference) findPreference(KEY_SHAKE_ENABLED);
		shake_enabled.setOnPreferenceClickListener(this);

	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		if (preference == rescan) {
			mHandler.sendEmptyMessage(PROCESS_STARTED);
			sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
					Uri.parse("file://"
							+ Environment.getExternalStorageDirectory())));
		} else if (preference == plugins_manager) {
			startActivity(new Intent(INTENT_PLUGINS_MANAGER));
		} else if (preference == shake_enabled) {
			MusicUtils.reloadSettings();
		}

		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		IntentFilter intentFilter = new IntentFilter(
				Intent.ACTION_MEDIA_SCANNER_STARTED);
		intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
		intentFilter.addDataScheme("file");
		registerReceiver(mReceiver, intentFilter);

	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(mReceiver);
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			progressDialog = new ProgressDialog(this);
			progressDialog.setMessage(getString(R.string.scanning));
			progressDialog.setCancelable(true);
			break;
		}
		return progressDialog;
	}

	private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
				mHandler.sendEmptyMessage(PROCESS_FINISHED);
			}
		}
	};
}
